package controller
{
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.FileFilter;
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	import model.EditeMapData;
	import model.MapModel;
	
	import mx.containers.Canvas;
	import mx.controls.Alert;
	import mx.core.UIComponent;
	
	import ricky.Interface.ILoadComplete;
	import ricky.animate.AnimSprite;
	import ricky.loader.Request;
	import ricky.loader.protocol.MapDecoder;
	import ricky.scene.MapData;
	import ricky.utils.Utils;
	
	import rickyGame.config.GameUnitHelper;
	import rickyGame.config.unit.Unit;
	
	import spark.components.Label;
	
	import view.CreateMapView;
	import view.EditeRole;
	import view.ListBox;
	import view.aimate.EditeAnimSprite;
	import view.mapEditView;

	public class Controller extends BaseController implements ILoadComplete
	{
		
		public function Controller()
		{
			mapModel=new MapModel();
			//加载配置文件
			GameUnitHelper.init();
		}
		
		override public function proccess(cmd:Object,par:*):void
		{
			switch(cmd.toString())
			{
				case "CreateMapView_sure"://确定
					var cmapView:CreateMapView=par;
					mapModel.mapData=cmapView.editeMap;
					mapModel.mapData.mapURLPath=cmapView.nativePath+cmapView.mapURLPath.replace("\\\\","\\").replace("\\\\","\\");
					this.showTips("加载地图");	
					loadeMap();
					break;
				case "View_Clear"://确定
					
					this.main.removeAllChildren();
					break;
				
				case "EditMapView"://创建新建地图视图
					this.main.removeAllChildren();
					var cmapView:CreateMapView=new CreateMapView();
					cmapView.baseController=this;
					this.main.addChild(cmapView);
					if(par is EditeMapData)
					{
						this.mapModel.mapData=par;
						cmapView.editeMap=mapModel.mapData;

					}
					break;
				case "save_map":
					saveMap();
					break;
				case "open_mapdata":
					open_mapdata();
					break;
				case "open_unitlist":
					open_unitList(par);
					break;
				case "choose_role":
					choose_role(par);
					break;
				case "edite_role":
					edite_role(par);
					break;
			}
		}
		
		/**
		 * 编辑角色
		 */
		private function edite_role(par:EditeAnimSprite):void
		{
			if(!_editerole)
			{
				_editerole=new EditeRole();
				_editerole.addEventListener("close",onEditeMenuClose);
			}
			
			_editerole.y=10;
			_editerole.x=277;
			if(this.mapView)
			{
				mapView.addChild(_editerole);
				_editerole.role=par;
			}
			
		}
		
		private function onEditeMenuClose(e:Event):void
		{
			if(this.mapView)
			{
				mapView.onEditeMenuClose();
				_editerole.role=null;
				_editerole.parent.removeChild(_editerole);
			}
		}
		
		/**
		 * 选择角色
		 */
		private function choose_role(uni:Unit):void
		{
			if(mapView)
			{
			var sp:EditeAnimSprite=new EditeAnimSprite();
			sp.name=uni.name;
			sp.unit=uni;
			sp.loadModel(uni.model);
			sp.playScene("stand_4");
//			this.ma
			mapView.chooseRole=sp;
			}
		}
		
		/**
		 * 打开单位列表
		 */
		private function open_unitList(type:String):void
		{
//			if(mapModel.mapData==null)
//			{
//				Alert.show("请先创建或打开地图数据!");
//				return;
//			}
			left.removeAllChildren();
			var unitList:ListBox=new ListBox();
			unitList.unitType=type;
			unitList.baseController=this;
			
			left.addChild(unitList);
		}
		
		private function open_mapdata():void
		{
			if(!mapPath)
				mapPath=new File("F:\\MyProject\\rickyPu\\client\\gameEngine\\src\\images\\map");
			var my:FileFilter=new FileFilter("Documents", "*.map"); 
			mapPath.browseForOpen("选择map文件路径",[my]);
			mapPath.addEventListener(Event.SELECT,onSelect);
		}
		
		private function onSelect(e:Event):void
		{
			mapPath=e.target as File;	
			nativePath=mapPath.nativePath;
			var fs:FileStream=new FileStream();
			fs.open(mapPath,FileMode.READ);
			var b:ByteArray=new ByteArray();
			b.endian=Endian.LITTLE_ENDIAN;
			fs.readBytes(b,0,fs.bytesAvailable);
			fs.close();
			var req:Request=new Request("");
			req.data=b;
			var mapdecoder:MapDecoder=new MapDecoder();
			mapdecoder.doDecoder(req,this);
		}
		
		/**
		 * 处理回调
		 * @param req:Request 请求
		 */
		public function doComplete(req:Request):void
		{
			var q:*=req.data;
			this.mapModel.mapData=new EditeMapData();
			mapModel.mapData.cloneFrom(q);
			var _nativePath:String=mapPath.nativePath.substring(0,mapPath.nativePath.lastIndexOf("\\"));
			_nativePath=_nativePath.substring(0,_nativePath.lastIndexOf("\\"));
			_nativePath=_nativePath.substring(0,_nativePath.lastIndexOf("\\"));
//			i=_nativePath.substring(0,i).lastIndexOf("\\",i);
			mapModel.mapData.nativePath=_nativePath+"\\";
			this.proccess("EditMapView",mapModel.mapData);
//			editeMap();
		}
		
		/**
		 * 保存地图数据
		 */
		public function saveMap():void
		{
			if(mapModel.mapData==null)
			{
				Alert.show("请先创建或打开地图数据!");
				return;
			}
				
			var md:EditeMapData=mapModel.mapData;
			var mapbyte:ByteArray=new ByteArray();
			md.monster=this.mapView.getRoles();		
			
			mapbyte.endian=Endian.LITTLE_ENDIAN;
			mapbyte.writeUTF(md.name);
			mapbyte.writeUTF(md.nativePath);
			mapbyte.writeUTF(md.discription);
			mapbyte.writeInt(md.width);
			mapbyte.writeInt(md.height);
			mapbyte.writeInt(md.gridWidth);
			mapbyte.writeInt(md.gridHeight);
			mapbyte.writeUTF(md.smallMapURL);	
			mapbyte.writeInt(md.data.length);
			for each(var v:* in md.data)
				mapbyte.writeByte(v);
			mapbyte.writeUTF(md.monster);//怪物配置
			mapbyte.writeUTF(md.aiScript);
			mapbyte.writeUTF(md.bornRect);
			mapbyte.writeInt(md.col);
			save(mapbyte,md.name+".map");
		}
		
		/**
		 * 保存文件
		 */
		public function save(byte:ByteArray,fileName:String):void
		{
			var choose:File=new File();
			choose.addEventListener(Event.COMPLETE,onSave);
			choose.save(byte,fileName);
		}
		
		private function onSave(e:Event):void
		{
			showTips("文件保存成功!"+new Date().toString());
		}

		/**
		 * 加载读取可编辑的地图s
		 */
		public function loadeMap():void
		{
			this.main.removeAllChildren();
			if(mapView)
				mapView.map.dispose();
			mapView=new mapEditView();
			mapView.baseController=this;
			this.main.addChild(mapView);
			mapView.mapData=mapModel.mapData;
			mapView.init();
			mapView.addRolose(mapModel.mapData.monster);
		}
	
		/**
		 * 操作提示
		 */
		public function showTips(msg:String):void
		{
			if(tips)
				tips.text=msg;
		}
		
		/** 主窗口 */
		public var main:Canvas;
		public var left:Canvas;
		public var tips:Label;
		public var mapView:mapEditView;
		
		public var mapModel:MapModel;
		
		//编辑地图文件
		public var  mapPath:File;
		public var nativePath:String;
		
		private var _editerole:EditeRole;
	}
}